<?php

declare(strict_types=1);

namespace app\core\Anotation;

use app\core\Route\Authorization;

use app\core\Request;
use Exception;

class Helper
{
    const NUMBER = 1;
    const STRING = 2;
    const ARRAY = 3;

    public static function typeCheck(string $name, int $type)
    {
       
        $message = '';
        $expectedType = '';
        $isChecked = false;
       

        switch ($type) {
            case static::NUMBER:

                $expectedType = 'number';
                if($name=='off_set'){
                   $cc= Request::param('off_set',0,'intval');
                   var_dump($cc);
                }
                if (!is_numeric(2)) {
                    $isChecked = true;
                }
                break;
            case static::STRING:

                $expectedType = 'string';
              
                if (!is_string(Request::param($name,'','trim'))) {
                    $isChecked = true;
                }
                break;
            case static::ARRAY:
                $expectedType = 'array';
                
                if (!is_array((array)Request::param($name))) {
                    $isChecked = true;
                }
                break;
            default:
                $isChecked = false;
                # code...
                break;
        }

        if ($isChecked) {
            $format = "%s must be %s";
            $message = sprintf($format, $name, $expectedType);
            static::popException($message);
        }
    }

    public static function requireCheck(string $name)
    {
            if (!array_key_exists($name,Request::param())) {
            $format = "%s required";
            $message = sprintf($format, $name);

            static::popException($message);

        }
       
    }
    public static function checkRouteAuth(bool $check = false)
    {
        if ($check) {
            Authorization::verify();
        }
    }
    public static function textCheck(string $name, int $min = 0, int $max = 0)
    {
        $value = Request::param($name,'','trim');
        // static::typeCheck($name,static::STRING);
        $length=strlen($value);
        if ($min > 0) {
            $format = "%s value size less then %d";
            $message = sprintf($format, $name, $min);
            if ($min > $length) {
                static::popException($message);
            }
        }
        if ($max > 0) {
            $format = "%s value size more then %d";
            $message = sprintf($format, $name, $max);

            if ($length > $max) {
                static::popException($message);
            }
        }
    }
    public static function numberCheck(string $name, int $min = 0, int $max = 0)
    {
        $value = strval(Request::param($name,0,'intval'));
      
        static::typeCheck($name,static::NUMBER);
        
        if ($min > 0) {
            $format = "%s value size less then %d";
            $message = sprintf($format, $name, $min);
            if (strlen($value) <$min  ) {
                
                static::popException($message);
            }
        }
        if ($max > 0) {
            $format = "%s value size more then %d";
            $message = sprintf($format, $name, $max);

            if (strlen($value) > $max) {
                static::popException($message);
            }
        }
       
    }
    public static function arrayCheck(string $name, int $min = 1, int $max = 0)
    {
        $value =(array)Request::param($name);
        static::typeCheck($name,static::ARRAY);
        if ($min > 0) {
            $format = "%s value size less then %d";
            $message = sprintf($format, $name, $min);
            if (count($value) <$min) {
                static::popException($message);
            }
        }
        if ($max > 0) {
            $format = "%s value size more then %d";
            $message = sprintf($format, $name, $max);

            if ($value > $max) {
                static::popException($message);
            }
        }
    }


    private static function popException(string $message)
    {
        throw new Exception(
            $message,
            406
        );
    }
}
